package com.jia.train.seat.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.extra.pinyin.PinyinUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jia.train.global.enums.BusinessEnum;
import com.jia.train.global.exception.BusinessException;
import com.jia.train.global.page.PageResponse;
import com.jia.train.global.utils.SnowIdUtil;
import com.jia.train.seat.entity.Station;
import com.jia.train.seat.entity.StationExample;
import com.jia.train.seat.mapper.StationMapper;
import com.jia.train.seat.request.StationQueryRequest;
import com.jia.train.seat.request.StationSaveRequest;
import com.jia.train.seat.response.StationQueryResponse;
import com.jia.train.seat.response.StationSaveResponse;
import com.jia.train.seat.service.StationService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class StationServiceImpl implements StationService {

    @Resource
    private StationMapper stationMapper;

    @Override
    public StationSaveResponse insert(StationSaveRequest request) {
        Station station = BeanUtil.copyProperties(request, Station.class);
        DateTime now = DateTime.now();
        String name = station.getName();
        if (ObjectUtil.isNotEmpty(name)) {
            station.setNamePinyin(PinyinUtil.getPinyin(name, ""));
            station.setNamePy(PinyinUtil.getFirstLetter(name, ""));
        }
        if (ObjectUtil.isEmpty(station.getId())) {
            if (getStationCount(name) > 0) {
                throw new BusinessException(BusinessEnum.STATION_ALREADY_EXISTS);
            }
            station.setId(SnowIdUtil.getLongSnowNextId());
            station.setCreateTime(now);
            stationMapper.insert(station);
        } else {
            station.setUpdateTime(now);
            stationMapper.updateByPrimaryKeySelective(station);
        }

        return BeanUtil.copyProperties(station, StationSaveResponse.class);
    }

    @Override
    public PageResponse<StationQueryResponse> selectPage(StationQueryRequest request) {

        StationExample stationExample = new StationExample();

        // 构造分页
        PageHelper.startPage(request.getPage(), request.getSize());
        List<Station> stations = stationMapper.selectByExample(stationExample);
        PageInfo<Station> pageInfo = new PageInfo<>(stations);

        List<StationQueryResponse> list
                = BeanUtil.copyToList(stations, StationQueryResponse.class);

        PageResponse<StationQueryResponse> pageResponse = new PageResponse<>();
        pageResponse.setTotal(pageInfo.getTotal());
        pageResponse.setList(list);
        return pageResponse;
    }

    @Override
    public List<StationQueryResponse> selectList() {
        StationExample stationExample = new StationExample();
        stationExample.setOrderByClause("name_pinyin asc");
        List<Station> stations = stationMapper.selectByExample(stationExample);
        return BeanUtil.copyToList(stations, StationQueryResponse.class);
    }

    @Override
    public void deleteStation(Long id) {
        stationMapper.deleteByPrimaryKey(id);
    }

    private long getStationCount(String name) {
        StationExample StationExample = new StationExample();
        StationExample.Criteria criteria = StationExample.createCriteria();
        criteria.andNameEqualTo(name);
        return stationMapper.countByExample(StationExample);
    }
}
